package com.cnunicom.controller;

import com.cnunicom.domain.Order;
import com.cnunicom.domain.Product;
import com.cnunicom.service.OrderService;
import com.cnunicom.service.ProductService;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author: dingziyuan
 * @Date: 2020/4/4/004 21:14
 * @Description: *
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private RestTemplate restTemplate;
    @Resource
    private OrderService orderService;
    @Resource
    private DiscoveryClient discoveryClient;
    @Resource
    private ProductService productService;//feign实现的服务调用

    /*
        使用feign实现服务调用
     */
    @RequestMapping("/save/{pid}")
    public Order orderInfo(@PathVariable Integer pid) {

        final Product product = productService.productInfo(pid);

        final Order order = new Order();
        order.setUid(1);
        order.setUsername("张三");
        order.setPid(pid);
        order.setName(product.getName());
        order.setPrice(product.getPrice());
        order.setNumber(1);

        return orderService.save(order);
    }

    /*
       ribbon负载均衡策略
    */
    /*@RequestMapping("/save/{pid}")
    public Order orderInfo(@PathVariable Integer pid) {

        *//*
            restTemplate服务调用的缺点
            1.代码可读性不好
            2.Service使用spring注入，但是调用却使用restTemplate，编程风格不统一
         *//*
        final Product product = restTemplate
                .getForObject("http://server-product/product/info/" + pid, Product.class);

        final Order order = new Order();
        order.setUid(1);
        order.setUsername("张三");
        order.setPid(pid);
        order.setName(product.getName());
        order.setPrice(product.getPrice());
        order.setNumber(1);

        return orderService.save(order);
    }*/

/*    @RequestMapping("/save/{pid}")
    public Order orderInfo(@PathVariable Integer pid) {

        *//*
            nacos注册中心获取服务地址
         *//*
        ServiceInstance instance = discoveryClient.getInstances("server-product").get(0);

        final Product product = restTemplate
                .getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/info/" + pid
                        , Product.class);

        final Order order = new Order();
        order.setUid(1);
        order.setUsername("张三");
        order.setPid(pid);
        order.setName(product.getName());
        order.setPrice(product.getPrice());
        order.setNumber(1);

        return orderService.save(order);
    }*/

/*    @RequestMapping("/save/{pid}")
    public Order orderInfo(@PathVariable Integer pid) {
        *//*
          硬编码，耦合度太高
          1.一旦服务提供者做了集群，服务调用者无法实现负载均衡
          2.一旦微服务变得越来越多，那么如何来管理这个服务清单
          解决方案：注册中心，使用nacos
         *//*
        final Product product = restTemplate
                .getForObject("http://localhost:8081/product/info/" + pid, Product.class);

        final Order order = new Order();
        order.setUid(1);
        order.setUsername("张三");
        order.setPid(pid);
        order.setName(product.getName());
        order.setPrice(product.getPrice());
        order.setNumber(1);

        return orderService.save(order);
    }*/
}
